//
//  SetVC.m
//  UHealth
//
//  Created by hanyou on 15/3/18.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "SetVC.h"
#import "NavView.h"
#import "PersonalVC.h"
#import "MealTimeVC.h"
#import "DLAVAlertView.h"
#import "FeedbackVC.h"
#import "iToast.h"

#import "MBProgressHUD.h"
#import <ShareSDK/ShareSDK.h>

#import "SDImageCache.h"
#import "UIImageView+WebCache.h"

@interface SetVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)  UITableView        *contentTableView;
@property (nonatomic, strong)  NSMutableArray     *dataArray;


@end

@implementation SetVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = kBacColor;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NavView *nav = [[NavView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kNavHeight) andTitleStr:kLocal(@"set") andVC:self];
    [self.view addSubview:nav];
    
    _dataArray = [NSMutableArray arrayWithObjects:
                  @[@{@"img":@"setting01",@"title":kLocal(@"personInfo")}],
                  @[@{@"img":@"setting02",@"title":kLocal(@"invitefriends")}],
//                  @[@{@"img":@"setting03",@"title":kLocal(@"mealTime")}],
                  @[@{@"img":@"setting05",@"title":kLocal(@"feedback")},@{@"img":@"setting06",@"title": kLocal(@"clearCache")},@{@"img":@"setting07",@"title":kLocal(@"exit")}], nil];
    
    _contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavHeight, WIDTH, HEIGHT - kNavHeight) style:UITableViewStyleGrouped];
    _contentTableView.delegate = self;
    _contentTableView.dataSource = self;
    [_contentTableView setSeparatorInset:UIEdgeInsetsZero];
    [self.view addSubview:_contentTableView];

    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)inviateFriends
{
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"logo120"  ofType:@"png"];
    NSArray *shareList = [ShareSDK getShareListWithType: ShareTypeSMS,ShareTypeWeixiSession,ShareTypeWeixiTimeline,ShareTypeQQ,ShareTypeMail, nil];
    id<ISSContent> publishContent = [ShareSDK content:@"U康体温"
                                       defaultContent:@"U康体温"
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"U康体温"
                                                  url:@"http://testapi.uwellnesshk.com/app/down_tw.html"
                                          description:@"U康体温"
                                            mediaType:SSPublishContentMediaTypeNews];
    [ShareSDK showShareActionSheet:nil
                         shareList:shareList
                           content:publishContent
                     statusBarTips:NO
                       authOptions:nil
                      shareOptions: nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                if (state == SSResponseStateSuccess)
                                {
                                    NSLog(@"分享成功");
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    NSLog(@"分享失败,错误码:%ld,错误描述:%@", (long)[error errorCode], [error errorDescription]);
                                    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//                                    hud.labelText = [error errorDescription];
                                    [[iToast makeText:[NSString stringWithFormat:@"%@",[error errorDescription]]]show];
                                    [hud hide:YES afterDelay:0.5];
                                }
                            }];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray[section] count];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID =@"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    NSArray *arr = _dataArray[indexPath.section];
    NSDictionary *dict = arr[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:[dict objectForKey:@"img"]];
    cell.textLabel.text = [dict objectForKey:@"title"];
    if ((indexPath.section == 2 && indexPath.row >= 1 )|| indexPath.section == 1) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        if (indexPath.section == 2 && indexPath.row == 1) {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1fM",(float)[[SDImageCache sharedImageCache] getSize]/1024/1024];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    [self presentViewController:[[PersonalVC alloc]init] animated:YES completion:nil];
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                    [self inviateFriends];
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 3:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [self presentViewController:[[MealTimeVC alloc]init] animated:YES completion:nil];
                }
                    break;
                case 1:
                {
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [self presentViewController:[[FeedbackVC alloc]init] animated:YES completion:nil];
                }
                    break;
                case 1:
                {
                    DLAVAlertView *alertView = [[DLAVAlertView alloc] initWithTitle:kLocal(@"sure_clear") message:nil delegate:nil cancelButtonTitle:kLocal(@"cancel") otherButtonTitles:kLocal(@"sure"), nil];
                    [alertView showWithCompletion:^(DLAVAlertView *alertView, NSInteger buttonIndex) {
                        if (buttonIndex == 1) {//清除缓存
                            [SDWebImageManager.sharedManager.imageCache clearMemory];
                            [SDWebImageManager.sharedManager.imageCache clearDisk];
                            [tableView reloadData];
                        }
                    }];
                    
                }
                    break;
                case 2:
                {
                    DLAVAlertView *alertView = [[DLAVAlertView alloc] initWithTitle:kLocal(@"exit") message:nil delegate:nil cancelButtonTitle:kLocal(@"cancel") otherButtonTitles:kLocal(@"sure"), nil];
                    [alertView showWithCompletion:^(DLAVAlertView *alertView, NSInteger buttonIndex) {
                        if (buttonIndex == 1) {
                            exit(0);
                        }
                    }];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 9;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
